﻿using d1Common;
using d1Common.Interface;
using d1Project.Tools;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

namespace d1Project.Modules.Common.doStorage.Services
{
    public class doStorageServiceBase 
    {
        public doStorageServiceBase()
        {
            this.tempRootPath = doServices.Instance.DoConfig.readConfigStringValue(doStorageModule.ModuleIdDefine, "Basic", "TempRootPath", null);
            if (this.tempRootPath == null) throw new Exception("配置文件中未定义TempRootPath");
            string _tmpRootPath = DoEnvironment.GetExecutePath();
            if (!_tmpRootPath.EndsWith("/")) _tmpRootPath = _tmpRootPath + "/";
            this.tempRootPath = this.tempRootPath.Replace("$", _tmpRootPath);
        }
        private string storageRootPath = null;
        private string storageRootUrl = null;
        private string tempRootPath = null;
        private string buildTempFileFullName(string _path)
        {
            return this.tempRootPath + "/" + _path;
        }

        public byte[] readTempFile(string _path)
        {
            string _fileFullPath = this.buildTempFileFullName(_path);
            return File.ReadAllBytes(_fileFullPath);
        }

        public string writeTempFile(string _moduleId, string _path, byte[] _content)
        {
            string _fileFullPath = this.buildTempFileFullName(_moduleId + "/" + _path);
            string _filePath = Path.GetDirectoryName(_fileFullPath);
            if (!Directory.Exists(_filePath)) Directory.CreateDirectory(_filePath);
            File.WriteAllBytes(_fileFullPath, _content);
            return _moduleId + "/" + _path;
        }
    }
}